import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpCilent {
    private Socket socket=null;
    public TcpCilent(String ServerIP,int Port) throws IOException {
        socket=new Socket(ServerIP,Port);
    }
    public void strat(){
        System.out.println("客户端启动");
        try(InputStream inputStream= socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream();
            Scanner scanner=new Scanner(inputStream);
            PrintWriter printWriter=new PrintWriter(outputStream);
            Scanner scannerIn=new Scanner(System.in)){
            while(true){
                //1.从控制台读取信息
                String request=scannerIn.next();
                //2.将数据发送给服务器
                printWriter.println(request);
                printWriter.flush();
                //3.从服务器获取响应
                if(!scannerIn.hasNext()){
                    break;
                }
                String response=scanner.next();
                //4.打印日志
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpCilent cilent=new TcpCilent("127.0.0.1",9090);
        cilent.strat();
    }
}
